package com.cruise.h5.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cruise.h5.mapper.CategoryMapper;
import com.cruise.h5.mapper.CustomizedLabelMapper;
import com.cruise.h5.mapper.RecommendLabelMapper;
import com.cruise.h5.pojo.entity.Category;
import com.cruise.h5.pojo.entity.CustomizedLabel;
import com.cruise.h5.pojo.entity.RecommendLabel;
import com.cruise.h5.pojo.vo.CategoryListVO;
import com.cruise.h5.pojo.vo.LabelListVO;
import com.cruise.h5.service.CategoryService;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cruise
 * @since 2021-03-25
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private  CategoryMapper categoryMapper;

    @Resource
    private CustomizedLabelMapper customizedLabelMapper;

    @Resource
    private RecommendLabelMapper recommendLabelMapper;

    @Override
    public List<CategoryListVO> categoryList() {

        ArrayList<CategoryListVO> categoryListVos = new ArrayList<>();

        List<Category> categories = categoryMapper.selectList(new QueryWrapper<Category>().lambda().eq(Category::getPid, 0).orderByAsc(Category::getSort));
        for(Category category : categories){
            CategoryListVO categoryListVO = new CategoryListVO();
            BeanUtils.copyProperties(category, categoryListVO);
            categoryListVos.add(categoryListVO);
        }

        for (CategoryListVO categoryListVO : categoryListVos){
            List<Category> fatherCategories = categoryMapper.selectList(new QueryWrapper<Category>().lambda().eq(Category::getPid, categoryListVO.getId()).orderByAsc(Category::getSort));
            List<CategoryListVO> categoryListVOList = Lists.transform(fatherCategories, (entity) -> {
                CategoryListVO vo = new CategoryListVO();
                BeanUtils.copyProperties(entity, vo);
                return vo;
            });
            categoryListVO.setCategoryListVOList(categoryListVOList);
        }
        return categoryListVos;
    }

    @Override
    public List<LabelListVO.RecommendLabelVO> recommedLabelList(String categoryId) {
        List<RecommendLabel> recommendLabels = recommendLabelMapper.selectList(new QueryWrapper<RecommendLabel>().lambda().eq(RecommendLabel::getCategoryId, categoryId).orderByAsc(RecommendLabel::getLabelSort));
        List<LabelListVO.RecommendLabelVO> recommendLabelVOList = Lists.transform(recommendLabels, (entity) -> {
            LabelListVO.RecommendLabelVO vo = new LabelListVO.RecommendLabelVO();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        });
        return recommendLabelVOList;
    }

    @Override
    public List<LabelListVO.CustomizedLabelVO> customizeLabelList(String userId) {
        List<CustomizedLabel> customizedLabels = customizedLabelMapper.selectList(new QueryWrapper<CustomizedLabel>().lambda().eq(CustomizedLabel::getUserId, userId).orderByAsc(CustomizedLabel::getLabelSort));
        List<LabelListVO.CustomizedLabelVO> recommendLabelVOList = Lists.transform(customizedLabels, (entity) -> {
            LabelListVO.CustomizedLabelVO vo = new LabelListVO.CustomizedLabelVO();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        });
        return recommendLabelVOList;
    }
}
